//
//  NewsViewController.m
//  SpiritWalk
//
//  Created by shange1999 on 16/3/11.
//  Copyright © 2016年 王鑫. All rights reserved.
//

#import "NewsViewController.h"



@interface NewsViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,retain)UITableView *tableviewSelf;
//@property(nonatomic,retain)UICollectionView *headScroPic; //头视图滚动
@property(nonatomic,retain)NSMutableArray *tableviewArr;
@property(nonatomic,retain)NSMutableArray *scrollerViewArr; //轮播图
@property(nonatomic,retain)WJS_PicScrollView *picScrollView;   //轮播图
@property(nonatomic,assign)NSInteger pageNumber;    //网址的参数
@end


@implementation NewsViewController

-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self =[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
      //  self.tableviewArr =[NSMutableArray array];
        //self.scrollerViewArr =[NSMutableArray array];
       // //self.pageNumber = 1;
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationController.navigationBar.translucent = NO;
    self.title = @"养生资讯";
    [self createView];
    [self createTBVData];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(scrollPush:) name:@"pushWebView" object:nil];
  
    [self loadSecondData];
}
#pragma 创建视图
-(void)createView{
    self.picScrollView =[[WJS_PicScrollView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 240)];
//    [self.view addSubview:self.picScrollView];
    
    self.tableviewSelf=[[UITableView alloc ]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-49) style:0];
    //这里用比例约束
    self.tableviewSelf.rowHeight = 132;
//    self.tableviewSelf.backgroundColor =[UIColor redColor];
    [self.view addSubview:self.tableviewSelf];
    self.tableviewSelf.separatorStyle = NO;
    self.tableviewSelf.delegate=self;
    self.tableviewSelf.dataSource=self;
    self.tableviewSelf.tableHeaderView = self.picScrollView;
    UINib *nib = [UINib nibWithNibName:NSStringFromClass([NewsTableViewCell class]) bundle:nil];    //找到nib文件
    [self.tableviewSelf registerNib:nib forCellReuseIdentifier:@"MyCell"];   //注册,在nib文件里面找到当前需要注册的cell
    static NSInteger page=1;
   
    
    self.tableviewSelf.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        page++;
        NSString *urlStr =[NSString stringWithFormat:@"http://www.ys137.com/api.php?act=getlist&catid=0&page=%ld&pagesize=20",page];
         [Tool GET:urlStr black:^(id data) {
             NSArray * dataArr=[NewsTableviewModel baseModelByArr:data[@"data"][@"article_list"]];
             for (NewsTableviewModel * model in dataArr) {
                 [self.tableviewArr addObject:model];
             }
             [self.tableviewSelf reloadData];
             [self.tableviewSelf.mj_footer endRefreshing];
         }];
        

        [Tool GET:@"http://www.ys137.com/api.php?act=getlist&catid=0&page=2&pagesize=20" black:^(id data) {

            [self.tableviewSelf reloadData];
            
        }];
 }];
    
    self.tableviewSelf.mj_header=[MJRefreshNormalHeader headerWithRefreshingBlock:^{
        page=1;
        [self createTBVData];
        [self.tableviewSelf.mj_header endRefreshing];

    }];
   
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (self.tableviewArr.count>0) {
         return self.tableviewArr.count;
    }else{
        NSLog(@"tableview没有数据");
        return 0;
    }
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NewsTableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:@"MyCell" forIndexPath:indexPath];
    cell.selectionStyle=NO;
    cell.receiveModel = self.tableviewArr[indexPath.row];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
        NewsVCDetailWebView *vc =[[NewsVCDetailWebView alloc]init];
    if (self.tableviewArr.count>0) {
        NewsTableviewModel  *model = self.tableviewArr[indexPath.row];
        vc.receiveID      = model.passID;
        [self.navigationController pushViewController:vc animated:YES];
    }else{
        NSLog(@"无网络");
    }
    
}

-(void)createTBVData{
    

[Tool GET:@"http://www.ys137.com/api.php?act=getlist&catid=0&page=1&pagesize=20" black:^(id data) {
    self.tableviewArr=[NewsTableviewModel baseModelByArr:data[@"data"][@"article_list"]];
    self.picScrollView.picScrollArr=[NewsTableviewModel baseModelByArr:data[@"data"][@"ad_data"]];
    [self.tableviewSelf reloadData];
}];
}

-(void)loadSecondData{
#pragma 上拉加载

}
#pragma 轮播图传递
-(void)scrollPush:(NSNotification*)notification{
    NewsVCDetailWebView *vc=[[NewsVCDetailWebView alloc]init];
    
    vc.receiveID =  [notification.userInfo objectForKey:@"passID"];
    [self.navigationController pushViewController:vc animated:YES];
}











- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
